package ModelLayer;
import java.util.*;
import java.text.SimpleDateFormat;
public class Lease
{
    private int id;
    private String currentDate;
    private String productBarcode;
    private int amount;
    private String period;
    private int employeeID;
    private int customerID;
    private boolean returned;
    private double totalPrice;

    private HashMap<Integer, Integer> products;

    public Lease(int id, String currentDate, String productBarcode, int amount, String period, int employeeID, int customerID){
        this.id = id;
        this.currentDate = currentDate;
        this.productBarcode = productBarcode;
        this.amount = amount;
        this.period = period;
        this.employeeID = employeeID;
        this.customerID = customerID;
        this.returned = false;
        products = new HashMap<Integer,Integer>();
    }

    public void addProduct(int productBarcode, int amount){
        if(ProductContainer.getInstance().findProductByBarcode(productBarcode).getLeasingPrice() > 0){
            if(products.containsKey(productBarcode)) {
                int modification = amount;
                modification += products.get(productBarcode);
                products.remove(productBarcode);
                products.put(productBarcode,modification);
                ProductContainer.getInstance().findProductByBarcode(productBarcode).setLeased(amount);
                CustomerContainer.getInstance().findCustomerById(customerID).setRented(amount);
                EmployeeContainer.getInstance().findEmployeeById(employeeID).setLeased(amount);
            } else {
                products.put(productBarcode, amount);
                ProductContainer.getInstance().findProductByBarcode(productBarcode).setLeased(amount);
                CustomerContainer.getInstance().findCustomerById(customerID).setRented(amount);
                EmployeeContainer.getInstance().findEmployeeById(employeeID).setLeased(amount);
            }
            double price = ProductContainer.getInstance().findProductByBarcode(productBarcode).getLeasingPrice();
            price *= amount;
            totalPrice += price;
            CustomerContainer.getInstance().findCustomerById(customerID).setIncome(price);
            EmployeeContainer.getInstance().findEmployeeById(employeeID).setIncome(price);
        }
    }

    public void removeProduct(int productBarcode, int amount){
        if(products.containsKey(productBarcode) && products.get(productBarcode) > amount){
            int modification = products.get(productBarcode) - amount;
            products.remove(productBarcode);
            ProductContainer.getInstance().findProductByBarcode(productBarcode).setLeased(-amount);
            CustomerContainer.getInstance().findCustomerById(customerID).setRented(-amount);
            EmployeeContainer.getInstance().findEmployeeById(employeeID).setLeased(-amount);
            products.put(productBarcode, modification);
            double price = ProductContainer.getInstance().findProductByBarcode(productBarcode).getLeasingPrice();
            price *= amount;
            totalPrice -= price;
            CustomerContainer.getInstance().findCustomerById(customerID).setIncome(-price);
            EmployeeContainer.getInstance().findEmployeeById(employeeID).setIncome(-price);
        }
    }

    public boolean onTime(){
        try{
            String dt = this.currentDate;  
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            Calendar c = Calendar.getInstance();

            c.setTime(sdf.parse(dt));

            c.add(Calendar.DATE, Integer.parseInt(this.period));  // number of days to add
            dt = sdf.format(c.getTime());  // dt is now the new date

            Date currentD = new Date();
            Date date = sdf.parse(dt);

            if(date.before(currentD)){
                return false;
            }else{
                return true;
            }

        }catch(Exception e){
            return false;
        }

    }

    public HashMap<Integer, Integer> getProducts(){
        return products;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCurrentDate() {
        return currentDate;
    }

    public void setCurrentDate(String currentDate) {
        this.currentDate = currentDate;
    }

    public String getProductBarcode() {
        return productBarcode;
    }

    public void setProductBarcode(String productBarcode) {
        this.productBarcode = productBarcode;
    }

    public boolean isReturned() {
        return returned;
    }

    public void setReturned(boolean returned) {
        this.returned = returned;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public String getPeriod() {
        return period;
    }

    public void setPeriod(String period) {
        this.period = period;
    }

    public int getEmployeeID() {
        return employeeID;
    }

    public void setEmployeeID(int employeeID) {
        this.employeeID = employeeID;
    }

    public int getCustomerID() {
        return customerID;
    }

    public void setCustomerID(int customerID) {
        this.customerID = customerID;
    }

    public double getTotalPrice()
    {
        return totalPrice;
    }

}
